package top.kaoshanji.s1ch09c;

import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.RequestScope;

/**
 * @author kaoshanji
 * ^_^
 * create time 2025/4/30 15:40
 */
@Component
@RequestScope
public class LoginProcessor {

    private final LoggedUserManagementService loggedUserManagementService;
    private final LoginCountService loginCountService;

    private String username;
    private String password;

    public LoginProcessor(LoggedUserManagementService loggedUserManagementService, LoginCountService loginCountService) {
        this.loggedUserManagementService = loggedUserManagementService;
        this.loginCountService = loginCountService;
    }

    /**
     * 代码清单-s1ch09c-198页b：每次用户登录就在应用作用域bean里递增
     * @return
     */
    public boolean login() {
        // 递增每次登录尝试的计数
        loginCountService.increment();

        boolean result = false;
        String userName = this.username;
        String passWord = this.password;

        if ("me123".equals(userName) && "ps456".equals(passWord)) {
            result = true;

            // 登录成功之后，将用户名存储在会话作用域 bean 中
            loggedUserManagementService.setUsername(username);
        }

        return result;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
